package com.xpsoft.oa.workflow.event;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.statistics.MeetingApply;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.statistics.MeetingApplyService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.DepartmentService;

public class MeetingApplyFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(MeetingApplyFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String applyUserId = (String)pi.getVariable("meetingApply.applyUser.userId");
		String depId = (String)pi.getVariable("meetingApply.dept.depId");
		String meetingTitle = (String)pi.getVariable("meetingApply.meetingTitle");
		String meetingType = (String)pi.getVariable("meetingApply.meetingType");
		Date startTime = (Date)pi.getVariable("meetingApply.startTime");
		Date endTime = (Date)pi.getVariable("meetingApply.endTime");
		String meetingLeaderId = (String)pi.getVariable("meetingApply.meetingLeader.userId");
		String meetingRecorderId = (String)pi.getVariable("meetingApply.meetingRecorder.userId");
		String attendUserNames = (String)pi.getVariable("meetingApply.attendUserNames");
		String attendUserIds = (String)pi.getVariable("meetingApply.attendUserIds");
		String content = (String)pi.getVariable("meetingApply.content");
		String attachIds = (String)pi.getVariable("meetingApply.attachIds");
		String attachFiles = (String)pi.getVariable("meetingApply.attachFiles");
		
		MeetingApply meetingApply = new MeetingApply();
		meetingApply.setMeetingTitle(meetingTitle);
		meetingApply.setMeetingType(meetingType);
		meetingApply.setStartTime(startTime);
		meetingApply.setEndTime(endTime);
		meetingApply.setAttendUserNames(attendUserNames);
		meetingApply.setContent(content);
		meetingApply.setAttachIds(attachIds);
		meetingApply.setAttachFiles(attachFiles);
		meetingApply.setStatus(0);//未完成
		AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
		if(StringUtils.isNotBlank(depId)){
			DepartmentService departmentService = (DepartmentService)AppUtil.getBean("departmentService");
			meetingApply.setDept(departmentService.get(Long.valueOf(depId)));
		}
		if(StringUtils.isNotBlank(applyUserId)){
			meetingApply.setApplyUser(appUserService.get(Long.valueOf(applyUserId)));
		}
		if(StringUtils.isNotBlank(meetingLeaderId)){
			meetingApply.setMeetingLeader(appUserService.get(Long.valueOf(meetingLeaderId)));
		}
		if(StringUtils.isNotBlank(meetingRecorderId)){
			meetingApply.setMeetingRecorder(appUserService.get(Long.valueOf(meetingRecorderId)));
		}
		Set<AppUser> attendUsers = null;
		if(StringUtils.isNotBlank(attendUserIds)){
			String attendIds[] = attendUserIds.split(",");
			attendUsers = new HashSet<AppUser>();
			for(String userId:attendIds){
				if(StringUtils.isNotBlank(userId)){
					attendUsers.add(appUserService.get(Long.valueOf(userId)));
				}
			}
			meetingApply.setAttendUsers(attendUsers);
		}
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		meetingApply.setProcessRunId(processRun.getRunId());
		
		MeetingApplyService meetingApplyService = (MeetingApplyService)AppUtil.getBean("meetingApplyService");
		meetingApplyService.save(meetingApply);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
		AppUtil.sendMsgByUserIds(attendUsers, meetingApply.getMeetingTitle()+"会议审批通过，请您准时参加会议。");
	}

}
